
var __zquery_document_loaded = false;
var __zquery_document_loaded_callbacks = [];


function ZQueryDOMCollection(selector){
    this._selector = selector;
    this.getDoms = function(){
        return document.querySelectorAll(this._selector);
    }

    this.text = function (t) {
        if(t){
            this.getDoms().forEach(d => d.innerText = t);
        }
        else{
            return this.getDoms()[0].innerText;
        }
    }

    this.val = function(v){
        if(v){
            this.getDoms().forEach(d => d.value = v);
        }
        else{
            return this.getDoms()[0].value;
        }
    }

    this.click = function(callback){
        this.getDoms().forEach(d => d.addEventListener("click",callback));
    }

    this.show = function(){
        this.getDoms().forEach(d => d.style.visibility="visible");
    }
    
    this.hide = function(){
        this.getDoms().forEach(d => d.style.visibility="hidden");
    }

    this.css = function(key,value){
        this.getDoms().forEach(d => d.style[key]=value);
    }
}

var $ = function(o){
    var type = typeof(o);
    if(type == "function"){
        if(__zquery_document_loaded){
            o();
        }
        else{
            __zquery_document_loaded_callbacks.push(o);
        }
    }
    if(type == "string"){
        return new ZQueryDOMCollection(o);
    }


}

window.addEventListener("load", function(){
    __zquery_document_loaded_callbacks.forEach(f => f());
    __zquery_document_loaded = true;
});


function objectToUrlParam(param){
    var searchstr="";
    var keys = Object.keys(param);
    for (var i = 0;i<keys.length;i++){
        searchstr = searchstr+keys[i];
        searchstr = searchstr+"=";
        searchstr = searchstr +encodeURI(param[keys[i]]);
        if(i<keys.length-1){
            searchstr = searchstr+"&";
        }
    }
    return searchstr;
}


$.post = function(url,param){
    var callback = new Promise(function(resolve,reject){

        var loginreq = new XMLHttpRequest();

        loginreq.onreadystatechange=function()
        {
            if (loginreq.readyState==4)
            {
                if(loginreq.status=200){
                    try{
                        var result = loginreq.responseText;
                        resolve(result);
                    }catch(e){
                        reject(e);
                    }
                }
                else{
                    reject(loginreq.status);
                }
            }
        }
    
        loginreq.open("POST",url,true);
        loginreq.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        loginreq.withCredentials=true;
        loginreq.send(objectToUrlParam(param));
    });
    return callback;
}

$.ajax = function(url,param){
    var callback = new Promise(function(resolve,reject){

        var loginreq = new XMLHttpRequest();

        loginreq.onreadystatechange=function()
        {
            if (loginreq.readyState==4)
            {
                if(loginreq.status=200){
                    try{
                        var result = loginreq.responseText;
                        resolve(result);
                    }catch(e){
                        reject(e);
                    }
                }
                else{
                    reject(loginreq.status);
                }
            }
        }
        var _url = url;
        if(param){
            _url = _url + "?"+objectToUrlParam(param);
        }
        loginreq.open("GET",_url,true);
        loginreq.withCredentials=true;
        loginreq.send();
    });
    return callback;
}

